import { Inject, Provide } from '@midwayjs/decorator';
import { BaseService } from '@cool-midway/core';
import { Context } from '@midwayjs/koa';
import { Repository } from 'typeorm';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { UserEntity } from '../../entity/user';
import { SchoolEntity } from '../../entity/school';
import { NewzfApiService } from '../newzf/api';
import { CourseService } from '../course';

/**
 * 路由
 */
@Provide()
export class RouterService extends BaseService {
    @Inject()
    ctx: Context;

    @Inject()
    nwezfapi: NewzfApiService;

    @Inject()
    courseService: CourseService;

    @InjectEntityModel(SchoolEntity)
    schoolEntity: Repository<SchoolEntity>;

    @InjectEntityModel(UserEntity)
    userEntity: Repository<UserEntity>;

    /**
     * 登录
     * @param id 学校ID
     * @param username 学号
     * @param password 密码
     */
    async login(id: number, username: string, password: string) {
        const { url, sysType, loginType } = await this.schoolEntity.findOneBy({ id });
        switch (sysType) {
            case 0:
                return await this.nwezfapi.login(id, url, loginType, username, password);
            case 1:
                return { success: false, message: '暂未适配该系统', data: null };
        }
    }

    /**
     * 课表查询
     * @param year 学年
     * @param term 学期
     * @param refresh 是否刷新
     */
    async get_course(year, term, refresh?) {
        const { schoolId, username, password } = await this.userEntity.findOneBy({ id: this.ctx.user.userId });
        const { url, sysType, loginType } = await this.schoolEntity.findOneBy({ id: schoolId });
        return await this.courseService.get_course(url, sysType, loginType, username, password, year, term, refresh);
    }

    /**
     * 成绩查询
     * @param year 学年
     * @param term 学期
     */
    async get_grade(year, term) {
        const { schoolId, username, password } = await this.userEntity.findOneBy({ id: this.ctx.user.userId });
        const { url, sysType, loginType } = await this.schoolEntity.findOneBy({ id: schoolId });
        switch (sysType) {
            case 0:
                return await this.nwezfapi.get_grade(url, loginType, username, password, year, term);
            case 1:
                return { success: false, message: '暂未适配该系统', data: null };
        }
    }

    /**
     * 通知查询
     * @param read 是否已读
     */
    async get_notify(read) {
        const { schoolId, username, password } = await this.userEntity.findOneBy({ id: this.ctx.user.userId });
        const { url, sysType, loginType } = await this.schoolEntity.findOneBy({ id: schoolId });
        switch (sysType) {
            case 0:
                return await this.nwezfapi.get_course_notify(url, loginType, username, password, read);
            case 1:
                return { success: false, message: '暂未适配该系统', data: null };
        }
    }

    /**
     * 阅读通知
     * @param id 通知ID
     */
    async read_notify(id) {
        const { schoolId, username, password } = await this.userEntity.findOneBy({ id: this.ctx.user.userId });
        const { url, sysType, loginType } = await this.schoolEntity.findOneBy({ id: schoolId });
        switch (sysType) {
            case 0:
                return await this.nwezfapi.read_notify(url, loginType, username, password, id);
            case 1:
                return { success: false, message: '暂未适配该系统', data: null };
        }
    }

    /**
     * 考试查询
     * @param year 学年
     * @param term 学期
     */
    async get_exam(year, term) {
        const { schoolId, username, password } = await this.userEntity.findOneBy({ id: this.ctx.user.userId });
        const { url, sysType, loginType } = await this.schoolEntity.findOneBy({ id: schoolId });
        switch (sysType) {
            case 0:
                return await this.nwezfapi.get_exam(url, loginType, username, password, year, term);
            case 1:
                return { success: false, message: '暂未适配该系统', data: null };
        }
    }
}